package com.example.onlineeducation.data.db.datastore


import androidx.datastore.preferences.core.booleanPreferencesKey
import androidx.datastore.preferences.core.stringPreferencesKey
import com.example.onlineeducation.data.constant.Constants
import com.tencent.bugly.crashreport.CrashReport
import com.yc.jetpacklib.data.datastore.YcDataStore

/**
 * Creator: sindi
 * Date: 2022/01/05
 * UseDes:用户信息 dataStore用的key
 */
object UserInfoDataStoreKey {
    val KEY_USER_PHONE_NUM = stringPreferencesKey("online_education_key_user_phone_num")
    val KEY_USER_TOKEN = stringPreferencesKey("online_education_key_user_token")
    val KEY_USER_NAME = stringPreferencesKey("online_education_key_user_neme")
    val KEY_USER_ROLE = stringPreferencesKey("online_education_key_user_role")//用户角色

    /**
     * 保存登录状态
     * @param phoneNum String?   手机号
     * @param token String      hcc账号token
     */
    suspend fun saveLoginState(phoneNum: String? = null, token: String? = null, userName: String? = null) {
        CrashReport.setUserId(phoneNum ?: "")
        YcDataStore.saveMore { preferences ->
            preferences[KEY_USER_TOKEN] = token ?: ""
            preferences[KEY_USER_PHONE_NUM] = phoneNum ?: ""
            preferences[KEY_USER_NAME] = userName ?: ""
        }
    }

    /**
     * 清除登录状态
     */
    suspend fun clearLoginState() {
        YcDataStore.saveMore {
            it[KEY_USER_PHONE_NUM] = ""
            it[KEY_USER_TOKEN] = ""
            it[KEY_USER_NAME] = ""
        }
    }
}